package com.Angaliwa.Whiteboard{
	import com.Angaliwa.Whiteboard;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import mx.core.UIComponent;
	import mx.effects.Fade;
	import mx.effects.easing.Quartic;
	
	public class Draw {
		public var enabled:Boolean=false,
				   color:String = "000000",
				   thickness:int = 1,
				   ignoreDistance:int = 5,
				   whiteboard:Whiteboard;
		
		private var layer:UIComponent,
					startX:int,
					startY:int,
					tempM:MovieClip;
		
		public function init():void{
			this.layer = whiteboard.getLayer();
			layer.cacheAsBitmap=true;
			
			// To avoid the drawing to 'stick' to the mouse when it leaves
			// the whiteboard, we'll finish the drawing when the mouse
			// leaves the whiteboard.
			layer.parent.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
			layer.parent.addEventListener(MouseEvent.MOUSE_UP, endDraw);
			layer.parent.addEventListener(MouseEvent.MOUSE_MOVE, drawing);
			layer.parent.addEventListener(MouseEvent.ROLL_OUT, endDraw);
		}
		
		public function startDraw(e:MouseEvent):void{
			if(enabled){
				startX = e.localX;
				startY = e.localY;
			}
		}
		
		public function drawing(e:MouseEvent):void{
			if(enabled && startX > 0 && startY > 0){
				if(!tempM){
					tempM = new MovieClip();
					layer.addChild(tempM);
				}
				
				tempM.graphics.clear();
				tempM.graphics.lineStyle(thickness,uint(color));
				tempM.graphics.moveTo(startX, startY);
				tempM.graphics.lineTo(e.localX, e.localY);
			}
		}
		
		public function endDraw(e:MouseEvent):void{
			if(enabled && startX > 0 && startY > 0){
				// To make sure the user actually drew something, and
				// did not accidentally click.
				if((e.localX > startX+ignoreDistance || e.localX < startX-ignoreDistance)
				   || (e.localY > startY+ignoreDistance || e.localY < startY-ignoreDistance)){
					
					var m:MovieClip = new MovieClip();
					m.graphics.lineStyle(thickness,uint(color));
					m.graphics.moveTo(startX, startY);
					m.graphics.lineTo(e.localX, e.localY);
					
					layer.addChild(m);
				}
				
				if(tempM){
					layer.removeChild(tempM);
					tempM = undefined;
				}
				
				
				if(whiteboard.settings.get('syncWithServer'))
					whiteboard.features.sync.a(layer.name, {feature:'draw',
											thickness:thickness,color:color,
											data:{startX:startX,startY:startY,endX:e.localX,endY:e.localY}});
				
				startX,startY=0;
			}
		}
		
		public function sync(layerName:String, d:Object):void{
			if(whiteboard.settings.get('syncWithServer')){
				var m:MovieClip = new MovieClip();
				m.graphics.lineStyle(d.thickness,uint(d.color));
				m.graphics.moveTo(d.data.startX, d.data.startY);
				m.graphics.lineTo(d.data.endX, d.data.endY);
				
				m.alpha=0;
				
				
				whiteboard.getLayer(layerName).addChild(m);
				
				var ani:Fade = new Fade(m);
				ani.alphaFrom=0;
				ani.alphaTo=1;
				ani.duration=500;
				ani.easingFunction = Quartic.easeIn;
				ani.play();
			}
		}
	}
}